package com.ljye.jg.ljye.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ljye.jg.ljye.App;
import com.ljye.jg.ljye.R;
import com.ljye.jg.ljye.bean.Login_Date;
import com.ljye.jg.ljye.utils.HttpUtils;
import com.ljye.jg.ljye.utils.Utils;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Activity_Register extends Activity {
    private ImageView back;//返回
    private EditText et_pwd;//输入密码
    private EditText phone;//输入手机号
    private Button smsPass;//验证码
    private EditText sms;//输入验证码
    private Button login;//注册
    private TextView xieyi;//协议
    private OkHttpClient client;
    public View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.back://返回
                    finish();
                    break;
                case R.id.sms_pass://获取验证码

                    break;
                case R.id.login://注册
                    register();
                    break;
                case R.id.xieyi://协议

                    break;
                default:
                    break;
            }
        }
    };
    private boolean isHidePwd = true;// 输入框密码是否是隐藏的，默认为true

    private void register() {
        final String mobile = phone.getText().toString().trim();
        final String password = et_pwd.getText().toString().trim();
        if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(password)) {
            Utils.showToast(Activity_Register.this, "用户名和密码不能为空哦！");
            return;
        }

        new Thread() {
            @Override
            public void run() {
                RequestBody body = new FormBody.Builder()
                        .add("mobile", mobile)//添加键值对
                        .add("password", password)
                        .build();
                Request request = new Request.Builder()
                        .url(HttpUtils.PATH + "/register")
                        .addHeader("Accept", HttpUtils.MYAPP)
                        .post(body)
                        .build();
                try {
                    Response execute = client.newCall(request).execute();
                    String string = execute.body().string();
                    Log.d("登录返回信息", string);
//                    int status_code = login_date.getStatus_code();
//                    if (status_code == 200) {
                    if (execute.isSuccessful()) {
                        Gson gson = new Gson();
                        Login_Date login_date = gson.fromJson(string, Login_Date.class);
                        //设置全局token
                        String token = login_date.getData().getToken().getToken();
                        App application = (App) getApplication();
                        application.setToken(token);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                login.setClickable(false);
                                Utils.showToast(Activity_Register.this, "注册成功！");
                            }
                        });

                        Intent intent = new Intent(Activity_Register.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                login.setClickable(true);
                                Utils.showToast(Activity_Register.this, "用户名存在！");
                            }
                        });

                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initEdit();
        client = App.getClient();
        back.setOnClickListener(listener);
        smsPass.setOnClickListener(listener);
        login.setOnClickListener(listener);
        xieyi.setOnClickListener(listener);


    }


    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        et_pwd = (EditText) findViewById(R.id.password);


        phone = (EditText) findViewById(R.id.phone);
        smsPass = (Button) findViewById(R.id.sms_pass);
        sms = (EditText) findViewById(R.id.sms);
        login = (Button) findViewById(R.id.login);
        xieyi = (TextView) findViewById(R.id.xieyi);

    }

    private void initEdit() {
        final Drawable[] drawables = et_pwd.getCompoundDrawables();
        final int eyeWidth = drawables[2].getBounds().width();// 眼睛图标的宽度
        final Drawable drawableEyeOpen = getResources().getDrawable(R.drawable.open_eyes);
        drawableEyeOpen.setBounds(drawables[2].getBounds());//这一步不能省略
        et_pwd.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    // getWidth,getHeight必须在这里处理
                    float et_pwdMinX = v.getWidth() - eyeWidth - et_pwd.getPaddingRight();
                    float et_pwdMaxX = v.getWidth();
                    float et_pwdMinY = 0;
                    float et_pwdMaxY = v.getHeight();
                    float x = event.getX();
                    float y = event.getY();
                    if (x < et_pwdMaxX && x > et_pwdMinX && y > et_pwdMinY && y < et_pwdMaxY) {
                        // 点击了眼睛图标的位置
                        isHidePwd = !isHidePwd;
                        if (isHidePwd) {
                            et_pwd.setCompoundDrawables(drawables[0],
                                    drawables[1],
                                    drawables[2],
                                    drawables[3]);

                            et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                        } else {
                            et_pwd.setCompoundDrawables(drawables[0],
                                    drawables[1],
                                    drawableEyeOpen,
                                    drawables[3]);
                            et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                        }
                    }
                }
                return false;
            }
        });
    }
}


